package com.awa.usercenter.model.domain;

import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用户表
 * @TableName user
 */
@TableName(value ="user")
@Data
@Accessors(chain = true)
public class User implements Serializable,UserDetails{
    /**
     * id
     * 主键
     */
    @TableId
    private long userId;
    /**
     * 用户密码
     */
    private String password;
    /**
     * 电话
     */
    private String phone;
    /**
     * 用户昵称
     */
    private String nickname;

    /**
     * 账号
     */
    private String email;


    /**
     * 性别
     * 0 - unknown
     * 1 - female
     * 2 - male
     */
    private int gender;

    /**
     * 用户头像
     */
    private String avatar;
    /**
     * 用户更新时间
     */
    private Date updateTime;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 工作岗位
     */
    private String job;
    /**
     * 微信
     */
    private String wechat;
    /**
     * QQ
     */
    private String qq;
    /**
     * 工作年限
     */
    private int workYear;
    /**
     * 工作目标
     */
    private String workAim;
    /**
     * 个性签名
     */
    private String description;
    /**
     * 地址
     */
    private String address;
    /**
     * 状态 0 - 正常
     * 状态 1 - 已封禁
     */
    private Integer userStatus;

    /**
     * 状态 0 - 未删除
     * 状态 1 - 已删除
     */
    @TableLogic
    private Integer isDelete;

    //不在表内
    /**
     * 角色信息
     */
    private Set<UserRole> roleSet;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<String> roleName = new HashSet<>();
        for (UserRole userRole : roleSet) {
            roleName.add(userRole.getRoleName());
        }
        if(!roleName.isEmpty()){
            return roleName.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
        }
        return null;
    }

    @Override
    public String getUsername()
    {
        return phone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}